// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Su Hao (suhao@baidu.com)
//
// Description: Baidu CA configuration.

#include "baidu_ca_conf.h"
#include "baidu_ca_internal.h"
#include "baidu_json.h"
#include "duer_log.h"

BCA_INT_IMPL bca_conf_handler bca_conf_create(const void* data, bca_size_t size) {
    DUER_LOGV("conf data: %s", data);
    return baidu_json_Parse((const char*)data);
}

BCA_INT_IMPL const char* bca_conf_get_string(bca_conf_handler hdlr,
        const char* key) {
    baidu_json* rs = baidu_json_GetObjectItem((baidu_json*)hdlr, key);

    if (rs) {
        DUER_LOGD("    bca_conf_get_string: %s = %s", key, rs->valuestring);
        return rs->valuestring;
    }

    return NULL;
}

BCA_INT_IMPL bca_u16_t bca_conf_get_ushort(bca_conf_handler hdlr, const char* key) {
    baidu_json* rs = baidu_json_GetObjectItem((baidu_json*)hdlr, key);

    if (rs) {
        DUER_LOGD("    bca_conf_get_string: %s = %d", key, rs->valueint);
        return (bca_u16_t)(rs->valueint & 0xFFFF);
    }

    return 0;
}

BCA_INT_IMPL bca_u32_t bca_conf_get_uint(bca_conf_handler hdlr, const char* key) {
    baidu_json* rs = baidu_json_GetObjectItem((baidu_json*)hdlr, key);

    if (rs) {
        DUER_LOGD("    bca_conf_get_string: %s = %d", key, rs->valueint);
        return (bca_u32_t)rs->valueint;
    }

    return 0;
}

BCA_INT_IMPL void bca_conf_destroy(bca_conf_handler hdlr) {
    baidu_json_Delete((baidu_json*)hdlr);
}
